#include "multirowheadertable.h"
#include <QHeaderView>
#include <QDebug>
#include <QAbstractSlider>
#include <QScrollBar>

MultiRowHeaderTable::MultiRowHeaderTable(QWidget *parent) : QTableWidget(parent)
{
    headerRowCount = 2;
    setColumnNum(2);
    contentRowCount = 1;
    headerRowHeight = 30;
    contentRowHeight = 20;
    initWidget();
}

QTableWidget *MultiRowHeaderTable::getHeaderTable()
{
    return headerTable;
}

QTableWidget *MultiRowHeaderTable::getContentTable()
{
    return this;
}

void MultiRowHeaderTable::resizeEvent(QResizeEvent *event)
{
    QTableView::resizeEvent(event);
    updateHeaderTableGeometry();

}

QModelIndex MultiRowHeaderTable::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
{
    QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);

    if (cursorAction == MoveUp && current.row() > 0
            && visualRect(current).topLeft().y() < headerTable->rowHeight(1) ){
          const int newValue = verticalScrollBar()->value() + visualRect(current).topLeft().y()
                               - headerTable->rowHeight(0) - headerTable->rowHeight(1);
          verticalScrollBar()->setValue(newValue);
    }
    return current;
}

void MultiRowHeaderTable::scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint)
{
    if (index.row() > 0)
        QTableView::scrollTo(index, hint);
}

void MultiRowHeaderTable::initWidget()
{
    initHeaderWidget();
    initContentWidget();
}

void MultiRowHeaderTable::setHeaderRowCount(int num)
{
    headerRowCount = num;
}

void MultiRowHeaderTable::setColumnNum(int num)
{
    headerColCount = num;
    contentColCount = num;
}

void MultiRowHeaderTable::setContentRowCount(int num)
{
    contentRowCount = num;
}

void MultiRowHeaderTable::setHeaderRowHeight(int height)
{
    headerRowHeight = height;
}

void MultiRowHeaderTable::setContentRowHeight(int height)
{
    contentRowHeight = height;
}

void MultiRowHeaderTable::initHeaderWidget()
{
    headerTable = new QTableWidget(this);

    //init header table    
    headerTable->setProperty("noinput", true);//不需要虚拟键盘
    headerTable->horizontalHeader()->hide();
    headerTable->verticalHeader()->hide();
//    headerTable->setShowGrid(false);    //网格线不可见
    headerTable->setEditTriggers(QAbstractItemView::NoEditTriggers);    //单元格不可编辑
    headerTable->setSelectionMode(QAbstractItemView::NoSelection);//不可选
    headerTable->horizontalHeader()->setStretchLastSection(true);//最后一个单元格扩展
    headerTable->setFocusPolicy(Qt::NoFocus);//解决选中虚框问题
    headerTable->setFrameShape(QFrame::NoFrame);//去除边框
    //隐藏滚动条
    headerTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    headerTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    headerTable->setHorizontalScrollMode(ScrollPerPixel);

    headerTable->setRowCount(headerRowCount);
    headerTable->setColumnCount(headerColCount);

    for (int r = 0;r < headerRowCount;r++) {
        headerTable->setRowHeight(r, headerRowHeight);
    }

    viewport()->stackUnder(headerTable);//设置窗口层次

    //连接信号槽-用于滚动条联动
    connect(headerTable->verticalScrollBar(), &QAbstractSlider::valueChanged,
            verticalScrollBar(), &QAbstractSlider::setValue);
    connect(verticalScrollBar(), &QAbstractSlider::valueChanged,
            headerTable->verticalScrollBar(), &QAbstractSlider::setValue);

    connect(headerTable->horizontalScrollBar(), &QAbstractSlider::valueChanged,
                horizontalScrollBar(), &QAbstractSlider::setValue);
    connect(horizontalScrollBar(), &QAbstractSlider::valueChanged,
            headerTable->horizontalScrollBar(), &QAbstractSlider::setValue);

    updateHeaderTableGeometry();//更新位置
//    headerTable->show();

    QString headerStyle = "background-color:qlineargradient(spread:pad,"
                          " x1:0, y1:0, x2:0, y2:1,"
                          " stop:0 #e7fafb,stop:0.6 #c9e9f5 ,stop:1 #c4deeb);";
    headerTable->setStyleSheet(headerStyle);

    headerTable->setItemDelegate(new ItemDelegate(0));//设置绘画代理（主要在代理中画出来header）
}

void MultiRowHeaderTable::initContentWidget()
{
    //init content table
    //不需要虚拟键盘
    setProperty("noinput", true);
    horizontalHeader()->setVisible(true);   //水平表头不可见
    verticalHeader()->hide();   //表头不可见
    //设置成被headerTable遮挡住
    int headerHeight = headerRowHeight*headerRowCount;
    horizontalHeader()->setFixedHeight(headerHeight);

//    setShowGrid(false);//网格线不可见
    setEditTriggers(QAbstractItemView::NoEditTriggers);//设置单元格不可编辑
//    setSelectionMode(QAbstractItemView::SingleSelection);//单选
//    setSelectionBehavior(QAbstractItemView::SelectRows);//选行
    horizontalHeader()->setStretchLastSection(true);//最后一个单元格扩展
    setFocusPolicy(Qt::NoFocus);//解决选中虚框问题
    setFrameShape(QFrame::NoFrame);//去除边框

    setVerticalScrollMode(ScrollPerPixel);
    setHorizontalScrollMode(ScrollPerPixel);

    //should set column count first
    setColumnCount(contentColCount);

//    setItemDelegate(new ItemDelegate(1));//设置绘画代理（主要在代理中画出来content）
}

void MultiRowHeaderTable::updateHeaderTableGeometry()
{
    qDebug() << "frameWidth():" << frameWidth() << "frameRect():" <<frameRect();
    qDebug() << "viewport()->width():" << viewport()->width() <<
                "horizontalHeader()->height():" <<horizontalHeader()->height();
    headerTable->setGeometry(frameWidth(),
                             frameWidth(),
                             viewport()->width(),
                             horizontalHeader()->height());
}
